package com.xindaoapp.happypet.utils;

import java.io.File;

import android.app.Activity;
import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

//import android.webkit.CacheManager;

/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : michael
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-8-31
 * 
 * 描述 : 缓存管理
 * 
 * 修订历史 :
 * 
 * ============================================================
 **/

public class xindaoCacheManager {
	static xindaoCacheManager instance;
	public static final xindaoCacheManager getInstance() {
		if (instance == null) {
			synchronized (xindaoCacheManager.class) {
				if (instance == null) {
					instance = new xindaoCacheManager();
				}
			}
		}
		return instance;
		
	}

	private int clearCacheFolder(File dir, long curTime) {          
	    int deletedFiles = 0;         
	    if (dir!= null && dir.isDirectory()) {             
	        try {                
	            for (File child:dir.listFiles()) {    
	                if (child.isDirectory()) {              
	                    deletedFiles += clearCacheFolder(child, curTime);          
	                }  
	                if (child.lastModified() < curTime) {     
	                    if (child.delete()) {                   
	                        deletedFiles++;           
	                    }    
	                }    
	            }             
	        } catch(Exception e) {       
	            e.printStackTrace();    
	        }     
	    }       
	    return deletedFiles;     
	}
	
	public static boolean isMethodsCompat(int VersionCode) {
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		return currentVersion >= VersionCode;
	}
	public void clearAppCache(final Activity activity)
	{
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					Toast.makeText(activity, "缓存清除成功", Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(activity, "缓存清除成功2", Toast.LENGTH_SHORT).show();					
//					Toast.makeText(activity, "缓存清除失败", Toast.LENGTH_SHORT).show();
				}
			}
		};
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					clearAppCache(activity.getApplication());
					msg.what = 1;
				} catch (Exception e) {
					e.printStackTrace();
					msg.what = -1;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	
	public void clearAppCache(Application application)
	{
		//清除webview缓存
//		File file = CacheManager.getCacheFileBaseDir();  
//		if (file != null && file.exists() && file.isDirectory()) {  
//		    for (File item : file.listFiles()) {  
//		    	item.delete();  
//		    }  
//		    file.delete();  
//		}  		  

		//清除数据缓存
		clearCacheFolder(application.getFilesDir(),System.currentTimeMillis());
		clearCacheFolder(application.getCacheDir(),System.currentTimeMillis());
		//2.2版本才有将应用缓存转移到sd卡的功能
		if(isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)){
			clearCacheFolder(MethodsCompat.getExternalCacheDir(application),System.currentTimeMillis());
		}
		
	}	
	
}
